<?php

require_once 'Interface.php';
require_once 'Config/Storage.php';

function makeConfig ($type, $storage_args)
{
	$config  = new RL_Config();
	$storage = RL_Config_Storage::create($type, $config, $storage_args);
	$config->setStorage($storage);

	return $config;
} // end of function makeConfig


/**
 * Класс RL_Config -- <{}>.
 * 
 * @package RL
 * @author Lucky <lucky@jetstyle.ru> 
 * @version $Id: Config.php 62 2007-09-05 08:27:21Z lucky $
 */
class RL_Config implements Config
{
	public $storage = NULL;
	public $path_separator = '/';
	public $childs = array();
	public $name = NULL;

	//* Config
	public function get ($key)
	{
		$o = $this->getStorage()->find($key);
		return $o;
	} // end of function get
	public function set ($key, $value)
	{
		$parts     = explode($this->getPathSeparator(), $key);
		$child_key = array_shift($parts);
		$cfg   = $this->getChild($child_key);

		if (is_null($cfg)) {
			$cfg = $this->buildConfig($child_key);
			$this->addChild($cfg);
		}

		if ($cfg instanceof Config) {
			return $cfg->set(implode('/', $parts), $value);
		} else {
			$this->childs[$child_key] = $value;
			return $this;
		}
	} // end of function set

	public function getStorage ()
	{
		return $this->storage;
	} // end of function getStorage
	public function setStorage ($value)
	{
		$this->storage = $value;
	} // end of function addStorage

	public function getPathSeparator ()
	{
		return $this->path_separator;
	} // end of function getPathSeparator
	public function setPathSeparator ($value)
	{
		$this->path_separator = $value;
	} // end of function setPathSeparator

	public function buildConfig ($name)
	{
		$cls = __CLASS__;
		$o   = new $cls();
		$o->setName($name);
		$o->setPathSeparator($this->getPathSeparator());
		$o->storage        = $this->storage;
		return $o;
	} // end of function buildConfig
}

